﻿using System;
using Netkk.Common.Models;

namespace Netkk.Models
{
    public class UserModel
    {
        public Guid Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Image { get; set; }
        public string Website { get; set; }
        public string Description { get; set; }
        public UserRole Role { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public bool IsValid { get; set; }
        public bool AllowShowProfile { get; set; }
        public bool AllowSendNewsletter { get; set; }
        public bool AllowSendEmails { get; set; }

        public bool AllowEdit { get; set; }
        public bool AllowAdd { get; set; }

        public static UserModel Create()
        {
            return new UserModel
                       {
                           Id = Guid.NewGuid()
                       };
        }

        public static UserModel Create(User user)
        {
            return new UserModel
                       {
                           Id = user.Id,
                           AllowSendEmails = user.AllowSendEmails,
                           AllowSendNewsletter = user.AllowSendNewsletter,
                           AllowShowProfile = user.AllowShowProfile,
                           Description = user.Description,
                           Email = user.Email,
                           FirstName = user.FirstName,
                           Image = user.Image,
                           IsValid = user.IsValid,
                           LastName = user.LastName,
                           Name = user.Name,
                           Password = user.Password,
                           Role = user.Role,
                           Website = user.Website
                       };
        }

        public static UserModel Create(User user, bool allowAdd, Func<Guid, bool> checkAllowEdit)
        {
            return new UserModel
                       {
                           Id = user.Id,
                           AllowSendEmails = user.AllowSendEmails,
                           AllowSendNewsletter = user.AllowSendNewsletter,
                           AllowShowProfile = user.AllowShowProfile,
                           Description = user.Description,
                           Email = user.Email,
                           FirstName = user.FirstName,
                           Image = user.Image,
                           IsValid = user.IsValid,
                           LastName = user.LastName,
                           Name = user.Name,
                           Password = user.Password,
                           Role = user.Role,
                           Website = user.Website,
                           AllowAdd = allowAdd,
                           AllowEdit = allowAdd || checkAllowEdit(user.Id)
                       };
        }

        public static UserModel Create(User user, bool allowAddOrEdit)
        {
            return new UserModel
            {
                Id = user.Id,
                AllowSendEmails = user.AllowSendEmails,
                AllowSendNewsletter = user.AllowSendNewsletter,
                AllowShowProfile = user.AllowShowProfile,
                Description = user.Description,
                Email = user.Email,
                FirstName = user.FirstName,
                Image = user.Image,
                IsValid = user.IsValid,
                LastName = user.LastName,
                Name = user.Name,
                Password = user.Password,
                Role = user.Role,
                Website = user.Website,
                AllowAdd = allowAddOrEdit,
                AllowEdit = allowAddOrEdit
            };
        }

        public override string ToString()
        {
            if (!String.IsNullOrEmpty(FirstName) || !String.IsNullOrEmpty(LastName))
            {
                return String.Format("{0} {1}", FirstName, LastName);
            }
            return Name;
        }

        public User MapTo()
        {
            return new User
               {
                   Id = Id,
                   AllowSendEmails = AllowSendEmails,
                   AllowSendNewsletter = AllowSendNewsletter,
                   AllowShowProfile = AllowShowProfile,
                   Description = Description,
                   Email = Email,
                   FirstName = FirstName,
                   Image = Image,
                   IsValid = IsValid,
                   LastName = LastName,
                   Name = Name,
                   Password = Password,
                   Role = Role,
                   Website = Website
               };
        }
    }
}
